package cn.licoy.wdog.core.service.hospital.impl;

import cn.licoy.wdog.common.exception.RequestException;
import cn.licoy.wdog.common.util.sendSmsCode.SendSmsCode;
import cn.licoy.wdog.common.util.sendSmsCode.SendSmsUtil;
import cn.licoy.wdog.common.util.sendSmsCode.SmsCheckCodeType;
import cn.licoy.wdog.core.dto.hospitalUser.UpdateHouitalUserDto;
import cn.licoy.wdog.core.entity.hospital.Smscode;
import cn.licoy.wdog.core.mapper.hospital.SmscodeMapper;
import cn.licoy.wdog.core.service.hospital.SmscodeService;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import org.apache.shiro.authc.DisabledAccountException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.Random;

/**
 * @author Sunne
 * Create time 2018/6/22 10:24
 * @return null
 **/
@Service
@Transactional
public class SmscodeServiceImpl extends ServiceImpl<SmscodeMapper,Smscode> implements SmscodeService {

    Logger logger = LoggerFactory.getLogger(SendSmsCode.class);
    @Override
    public String sencode(String phone, String codeType) {
        String content = "短信验证码为：";
        if (codeType.equals(SmsCheckCodeType.userRegister.getValue())) {
            content = "医生注册验证码为：";
        };
        if (codeType.equals(SmsCheckCodeType.userResetPwd.getValue())) {
            content = "用户重置密码验证码为：";
        }
        Random r=new java.util.Random();
        int rcode = r.nextInt(900000)+100000;

        Smscode smscode = new Smscode();
        smscode.setCode(Long.toString(rcode));
        smscode.setCreateDate(new Date());
        smscode.setPhone(phone);
        smscode.setState(codeType);

        this.insert(smscode);
        content = content + rcode;
        int resultCode = SendSmsUtil.sendSMS(phone, content);
        if (resultCode >= 0) {
            logger.info("验证码："+rcode+"成功发送");
            return Long.toString(rcode);
        }else{
            logger.info("验证码："+rcode+"发送失败,状态码："+resultCode);
            return "-1";
        }
    }
    @Override
    public String verificationCode(String phone, String code, String codeType) {
        if ("".equals(phone)||phone==null){
            throw RequestException.fail("电话不能为空");
        }

        if ("".equals(codeType)||codeType==null){
            throw RequestException.fail("验证码类型不能为空");
        }

        if ("".equals(code)||code==null){
            throw RequestException.fail("验证码不能为空");
        }
        Smscode scc = new Smscode();
        try {
            scc = this.selectOne(new EntityWrapper<Smscode>()
                    .setSqlSelect("id,code,create_date,phone,state")
                    .eq("phone",phone)
                    .eq("code",code)
                    .eq("state",codeType)
                    );
        }catch (RequestException e){
            throw RequestException.fail("验证不通过！",e);
        }
        if (scc==null){
            throw RequestException.fail("验证不通过！");
        }else {
            return "1";
        }

    }

    @Override
    public void updateUserInfo(UpdateHouitalUserDto dto) {

    }


}
